package com.wise.service.cms.content.column;

import cn.hutool.core.bean.BeanUtil;
import com.wise.entities.content.ArticleColumn;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.session.GetAcceptLanguage;
import com.wise.model.content.enums.ArticleEnum;
import com.wise.summer.v1.cms.content.column.AddColumn;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_001600250020;
import static com.wise.model.content.ContentErrMsgs.Err_001600250021;

@Service
@SummerService
public class AddColumnService implements SummerServiceBean<AddColumn> {
    @Override
    public void sum(AddColumn model) {
        ArticleEnum articleEnum = ArticleEnum.getThisFromType(model.getType());
        if (articleEnum == null) {
            throw new LogicException(this.getClass(), Err_001600250020);
        }
        if (!articleEnum.isHasColumn()) {
            throw new LogicException(this.getClass(), Err_001600250021);
        }
        String lang = GetAcceptLanguage.s(true);
        ArticleColumn articleColumn = new ArticleColumn();
        BeanUtil.copyProperties(model, articleColumn);
        articleColumn.setLang(lang);
        articleColumn.setAlias(model.getAlias());
		articleColumn.setCreatedBy(CurrentUserId.s(true));
		articleColumn.setCreationDate(DateTimeUtil.timestampNow());
        DaoSave.s(articleColumn);
        model.setSummerResult(articleColumn);
    }
}
